package com.shupian.controller;

import com.shupian.domain.ResponseResult;

import com.shupian.service.ArticleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;

@Api(tags = "文章相关接口")
@RestController
@RequestMapping("/article")
@Validated
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    //查询热门文章
    @ApiOperation(value = "查询热门文章列表",notes = "查询浏览量最高的前10篇文章的信息")

    @GetMapping("/hotArticleList")
    public ResponseResult hotArticleList(){
        //查询热门文章，分装成ResponseResult返回
        ResponseResult result=articleService.hotArticleList();
        return result;
    }

    @ApiOperation(value = "分页查询文章列表",notes = "①只能查询正式发布的文章 ②置顶的文章要显示在最前面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",required = true,paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "每页大小",required = true,paramType = "query",dataType = "Integer"),
            @ApiImplicitParam(name = "categoryId",value = "分类id",required = false,paramType = "query",dataType = "Long")
    })
    @GetMapping("/articleList")
    public ResponseResult articleList(
            @Min(1) Integer pageNum,
            @Min(1) Integer pageSize,
            @Min(1) Long categoryId){
        return articleService.articleList(pageNum,pageSize,categoryId);
    }

    @ApiOperation(value = "获取文章详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文章id",required = true,paramType = "path",dataType ="Long" )
    })
    //获取文章详细信息
    @GetMapping("/{id}")
    public ResponseResult getArticleDetail(@PathVariable("id") @Min(1) Long id){
        return articleService.getArticleDetail(id);
    }

    //更新浏览次数
    @ApiOperation(value = "更新浏览次数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章id", required = true, paramType = "path", dataType = "Long")
    })
    @PutMapping("/updateViewCount/{id}")
    public ResponseResult updateViewCount(@PathVariable("id") @Min(1) Long id){
        return articleService.updateViewCount(id);
    }
}


